import time
from dpkt.ethernet import *
from dpkt.ip import *
from ressources.datatypes import *

class TCPFlow():
    def __init__(self,sip,sport,dip,dport,start,size,state=0):
        '''
        TCP Flow object
            myflow = TCPFLow(source IP, source port, destination IP,
            destination port,start timestamp, first segment size,initial state)
        '''
        self.sip = sip
        self.sport = sport
        self.dip = dip
        self.dport = dport
        self.state = state      # TCP Flow state: 0=Unknown, 1=Establishing, 2=Connected, 9=Closed
        self.start = start      # First segment timestamp
        self.end = start        # Last segment seen timestamp
        self.size = size        # Total flow size in bytes
        
    def close(self,ts,size=0):
        '''Marks the Flow as closed'''
        self.end = ts
        self.size += size
        self.state = 9
        
    def update(self,ts,size,state):
        '''Updates end timestamp, size and state'''
        self.end = ts
        self.size += size
        self.state = state
        
    def getkey(self):
        '''Returns the flow ID sourceIP+sourcePort+destinationIP+destinationPort'''
        return self.sip + bytes(self.sport) + self.dip + bytes(self.dport)

class OutputActiveFlows():
    def __init__(self):
        self.__moduleName = "Active Flows Grapher"
        self.__flows = {
                        ETH_TYPE_IP:{},
                        ETH_TYPE_IP6:{}
                        }
        self.__flowkeys = {
                           IP_PROTO_TCP:[],
                           IP_PROTO_UDP:[]
                           }
        
    def push(self,pktlen,pkt,ts):
        print "Pushed one frame"
        
    def getflows(self,proto=ETH_TYPE_IP):
        return self.__flows[proto]
        
    def __parserBroker(self,pkt):
        frame = Ethernet(pkt)
        
    
        